// pages/billlist/billlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billList: [{
      "Id": 20,
      "Name": "321",
      "Remark": "无",
      "Payer": "user_test",
      "Date": "2018-08-13",
      "TotalValue": 12300,
      "LeftPercentage": 100,
      "Timestamp": 1534144309,
      "Amount": 100
    }, ],
    checkStatus: false
  },

  checkBill: function(id) {
    return true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  bindSwitchChange: function(e) {
    var app = getApp()

    console.log(e.detail.value)
    if (e.detail.value) {
      this.data.billList.forEach((item, index) => {
        if (item.Id === parseInt(e.target.id)) {
          if (app.checkBillIsInProcess(e.target.id) < 0) {
            app.globalData.billsInProcess.push({
              id: item.Id,
              name: item.Name,
              value: item.TotalValue,
              left: item.LeftPercentage,
              consume: item.LeftPercentage,
              amount: item.Amount
            })
          }
          return
        }
      })
    } else {
      app.globalData.billsInProcess.forEach((item, index) => {
        if (item.id === parseInt(e.target.id)) {
          app.globalData.billsInProcess.splice(index, 1)
          return
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    var app = getApp()
    wx.request({
      url: app.globalData.apiUrl + '/get_bills',
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.statusCode === 200) {
          // console.log(res.data.data)
          var tmpBillList = []
          res.data.data.forEach((item, index) => {
            item.isInProcess = app.checkBillIsInProcess(item.Id)
            tmpBillList.push(item)
            // console.log(item)
          })
          that.setData({
            billList: tmpBillList
          })
        } else {
          wx.showToast({
            title: '服务器错误 billlist ' + res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})